package com.qeng.aiwanba.util;

import android.content.Context;

import com.github.lzyzsd.jsbridge.CallBackFunction;

import org.json.JSONArray;

import java.util.List;

import cn.idianyun.streaming.DianyunSdk;
import cn.idianyun.streaming.listener.OnCheckPlayableListener;

/**
 * Created by lixy on 2017/10/11.
 */

public class MyOnCheckPlayableListener implements OnCheckPlayableListener {
    private static MyOnCheckPlayableListener instance;
    private CallBackFunction callBackFunction;
    private Context context;

    public static MyOnCheckPlayableListener getInstance(Context context,CallBackFunction function){
        if(instance == null){
            instance = new MyOnCheckPlayableListener(function);
        }else {
            instance.setCallBackFunction(function);
        }
        instance.setContext(context);
        return instance;
    }

    public void setContext(Context context){
        this.context = context;
    }

    public void setCallBackFunction(CallBackFunction function){
        this.callBackFunction = function;
    }

    public MyOnCheckPlayableListener(CallBackFunction function){
        this.callBackFunction = function;
    }

    @Override
    public void onCheckPlayable(List<Integer> adIds, List<Boolean> playableList) {
        JSONArray array = new JSONArray();
        for (int i = 0; i < adIds.size(); i++) {
            boolean result = playableList.get(i);
            if(result) {
                array.put(String.valueOf(adIds.get(i).intValue()));
            }
        }
        callBackFunction.onCallBack(array.toString());
        DianyunSdk.getInstance().unregisterPlayableListener(instance);
    }

}
